const router = require("koa-router")();
const rpn = require("request-promise-native");
const { StringDecoder } = require("string_decoder");
const cheerio = require("cheerio");
const tough = require("tough-cookie");
let dsCookie = "JSESSIONID=24B6BFE04BABB140919826BB5B08FA68";
var nodejieba = require("@node-rs/jieba");
var PDFParser = require("pdf2json");
const fs = require("fs");

router.prefix("/ds");

const headers = {
  Accept: "application/json, text/javascript, */*; q=0.01",
  "Accept-Encoding": "gzip, deflate, br",
  "Accept-Language": "zh-CN,zh;q=0.9",
  Connection: "keep-alive",
  "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  Cookie: dsCookie,
  Host: "d.sxhlsc.com",
  Origin: "https://d.sxhlsc.com",
  Referer:
    "https://d.sxhlsc.com/wit_time_line?menuId=09ef2c76bbad4181a9b7941fa41d4c42&pMenuId=cde428ce38734cdb9c6204d014ae11ed&backMenuId=09ef2c76bbad4181a9b7941fa41d4c42&deptId=&committeeId=b4880460fe934efa92b19350880c976d",
  "Sec-Fetch-Dest": "empty",
  "Sec-Fetch-Mode": "cors",
  "Sec-Fetch-Site": "same-origin",
  "sec-ch-ua":
    '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
  "sec-ch-ua-mobile": "?0",
  "sec-ch-ua-platform": "macOS",
  "User-Agent":
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
};

const getcookie = async () => {
  const options = {
    method: "POST",
    uri: "https://d.sxhlsc.com/loginAction",
    headers: {
      ...headers,
      Cookie: undefined,
      Referer: "https://d.sxhlsc.com/login",
    },
    form: {
      loginName: "jhjddgw",
      password: "Jhjddgw19210701",
    },
    transform: (body, res, resolveWithFullResponse) => {
      let str = res["headers"]["set-cookie"][0];
      dsCookie = str.replace(" Path=/; HttpOnly", "");
    },
  };
  await rpn(options);
};
router.get("/loginAction", async (ctx, next) => {
  const options = {
    method: "POST",
    uri: "https://d.sxhlsc.com/loginAction",
    headers: {
      ...headers,
      Cookie: undefined,
      Referer: "https://d.sxhlsc.com/login",
    },
    form: {
      loginName: "jhjddgw",
      password: "Jhjddgw19210701",
    },
    transform: (body, res, resolveWithFullResponse) => {
      let str = res["headers"]["set-cookie"][0];
      dsCookie = str.replace(" Path=/; HttpOnly", "");
    },
  };
  await rpn(options);
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
const getJ = async (url, form, Referer) => {
  await getcookie();
  const options = {
    method: "POST",
    uri: url,
    headers: {
      ...headers,
      Referer: Referer,
      Cookie: dsCookie,
    },
    form: form,
  };
  const rpnbody = await rpn(options);
  return rpnbody;
};

router.get("/getDxz", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/xnyhsj/getDxz",
    {
      key: newquery.key,
      dxId: "",
    },
    "https://d.sxhlsc.com/wit_time_line" +
      ctx.request.url.replace("/ds/getDxz", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});

router.get("/applymenu", async (ctx, next) => {
  const url = "https://d.sxhlsc.com/applymenu/getJ";
  const rpnbody = await getJ(
    url,
    {
      pId: "cde428ce38734cdb9c6204d014ae11ed",
    },
    url
  );
  let retdata = JSON.parse(rpnbody).data;
  retdata = retdata[0].list;
  if (retdata) {
    const newdata = [];
    retdata.forEach((k) => {
      newdata.push({
        ...k,
        f2: k.f2.replace("/mh82", ""),
      });
    });
    ctx.body = {
      code: 200,
      data: newdata,
    };
  } else {
    ctx.body = rpnbody;
  }
});
router.get("/getDetail", async (ctx, next) => {
  const url = "https://d.sxhlsc.com/applymenu/getJ";
  const rpnbody = await getJ(
    url,
    {
      pId: "4bfe53be8f18410cba50860f08dca61f",
    },
    url
  );
  let retdata = JSON.parse(rpnbody).data;
  retdata = retdata[0].list;
  if (retdata) {
    const newdata = [];
    retdata.forEach((k) => {
      if (k.f2.indexOf("/mh82") != -1) {
        newdata.push({
          ...k,
          type: "图文",
          f2: k.f2.replace("/mh82", ""),
        });
      }
      if (k.f2.indexOf("/mh162") != -1) {
        newdata.push({
          ...k,
          type: "视频",
          f2: k.f2.replace("/mh162", ""),
        });
      }
    });
    ctx.body = {
      code: 200,
      data: newdata,
    };
  } else {
    ctx.body = rpnbody;
  }
});
router.get("/getJJ", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbody = await getJ(
    "https://d.sxhlsc.com" + newquery.du2,
    ctx.request.query,
    "https://d.sxhlsc.com" +
      ctx.request.url +
      newquery.du2.replace("portalcontentrinfo/getJ", "")
  );
  const retdata = JSON.parse(rpnbody).data;
  ctx.body = {
    code: 200,
    data: retdata,
  };
});
router.get("/getJJdel", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbody = await getJ(
    "https://d.sxhlsc.com" + newquery.du,
    { ...ctx.request.query, page: 2, pageSize: 12, id: newquery.id },
    "https://d.sxhlsc.com" +
      ctx.request.url +
      newquery.du.replace("portalcontentrinfo/getJ", "")
  );
  const retdata = JSON.parse(rpnbody).data;
  ctx.body = {
    code: 200,
    data: retdata,
  };
});
router.get("/getVideoJ", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbody = await getJ(
    "https://d.sxhlsc.com" + newquery.du,
    { ...ctx.request.query, page: 1, pageSize: 24 },
    "https://d.sxhlsc.com/mh162" + ctx.request.url.replace("/ds/getVideoJ", "")
  );
  const retdata = JSON.parse(rpnbody).data;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com" + newquery.du2,
    { ...ctx.request.query, page: 2, pageSize: 12, id: retdata[0].c1 },
    "https://d.sxhlsc.com/mh162" + ctx.request.url.replace("/ds/getVideoJ", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: { list: retdata, firstVideo: retdataVideo },
  };
});
router.get("/getVideoOption", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com" + newquery.du2,
    { ...ctx.request.query, page: 2, pageSize: 12, id: newquery.id },
    "https://d.sxhlsc.com/mh162" +
      ctx.request.url.replace("/ds/getVideoOption", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/getContentListBySectionCodeNew", async (ctx, next) => {
  const newquery = ctx.request.query;
  await getcookie();
  const options = {
    method: "Get",
    uri: "https://d.sxhlsc.com/portalcontentr/getContentListBySectionCodeNew?sectionCode=wdjs&page=0&pageSize=0",
    headers: {
      ...headers,
      Cookie: dsCookie,
      Referer:
        "https://d.sxhlsc.com/wit_dsyj_list" +
        ctx.request.url.replace("/ds/getContentListBySectionCodeNew", ""),
    },
    form: undefined,
  };
  const rpnbody = await rpn(options);
  const retdataVideo = JSON.parse(rpnbody).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/getContentInfo", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/portalcontentr/getContentInfo",
    {
      contentId: newquery.id,
    },
    "https://d.sxhlsc.com/wit_wdjs_detail" +
      ctx.request.url.replace("/ds/getContentInfo", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/getContentListBySectionCodeLast", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/portalcontentr/getContentListBySectionCodeLast",
    {
      sectionCode: "lssj",
      page: 0,
      pageSize: 10,
    },
    "https://d.sxhlsc.com/wit_lssj_list" +
      ctx.request.url.replace("/ds/getContentListBySectionCodeLast", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/getDetailListBySectionCode", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/portalcontentr/getDetailListBySectionCode",
    {
      sectionCode: "hszp",
      page: 0,
      pageSize: 0,
    },
    "https://d.sxhlsc.com/wit_dsyj_list" +
      ctx.request.url.replace("/ds/getDetailListBySectionCode", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/dsyj", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/portalcontentr/getContentListBySectionCode",
    {
      sectionCode: "dsyj",
      page: 0,
      pageSize: 0,
    },
    "https://d.sxhlsc.com/wit_dsyj_list" +
      ctx.request.url.replace("/ds/dsyj", "")
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/getListByPIdAndOrganization", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/applymenu/getListByPIdAndOrganization",
    {
      pId: "bd2a3d6e19cc4be59db8c18d287ca9a7",
      searchVal: "",
      page: 0,
      pageSize: 0,
      branchId: "",
      committeeId: "b4880460fe934efa92b19350880c976d",
    },
    "https://d.sxhlsc.com/wit_dskt_dsgs?menuId=bd2a3d6e19cc4be59db8c18d287ca9a7&pMenuId=cde428ce38734cdb9c6204d014ae11ed&backMenuId=bd2a3d6e19cc4be59db8c18d287ca9a7&deptId=&committeeId=b4880460fe934efa92b19350880c976d"
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
router.get("/getContentListBySectionCode", async (ctx, next) => {
  const newquery = ctx.request.query;
  const rpnbodyVideo = await getJ(
    "https://d.sxhlsc.com/portalcontentr/getContentListBySectionCode?sectionCode=" +
      newquery.id,
    { page: 0, pageSize: 20 },
    "https://d.sxhlsc.com/wit_dskt_xkj?menuId=3e149884713b4d30b16462e5acdec1d9&backMenuId=bd2a3d6e19cc4be59db8c18d287ca9a7&branchId=&committeeId=b4880460fe934efa92b19350880c976d&indexUrl=&flagg=&isCommitteeMember=&pMenuId=cde428ce38734cdb9c6204d014ae11ed&name="
  );
  const retdataVideo = JSON.parse(rpnbodyVideo).data;
  ctx.body = {
    code: 200,
    data: retdataVideo,
  };
});
module.exports = router;
